package com.jjshop.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.TextUtils;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jjshop.R;
import com.jjshop.bean.TabEntity;
import com.jjshop.ui.fragment.HomeFragment;
import com.jjshop.ui.fragment.MyCenterFragment;
import com.jjshop.ui.fragment.MyShopFragment;
import com.jjshop.ui.fragment.ShopCarFragment;
import com.jjshop.utils.PreUtils;
import com.jjshop.widget.NoScrollViewPager;

import java.util.ArrayList;

public class HomeActivity extends BaseActivity {


    private NoScrollViewPager noScrollViewPager;
    private CommonTabLayout commonTabLayout;
    private String[] mTitles = {"首页", "我的店", "购物车", "个人中心"};
    private int[] mIconUnselectIds = {
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    private int[] mIconSelectIds = {
            R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,
            R.mipmap.ic_launcher};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int setLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        super.initView();
        noScrollViewPager = findViewById(R.id.no_scroll_viewpager);
        commonTabLayout = findViewById(R.id.commonTabLayout);
    }

    @Override
    protected void initData() {
        super.initData();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        noScrollViewPager.setOffscreenPageLimit(3);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
                noScrollViewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));


                commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
                    @Override
                    public void onTabSelect(int position) {
                        String string = PreUtils.getString(HomeActivity.this, PreUtils.SHOP_ID, "");
                        if (!TextUtils.isEmpty(string)){
                            noScrollViewPager.setCurrentItem(position, false);
                        }else{
                            commonTabLayout.setCurrentTab(0);
                        }
//                        switch (position) {
//                            case 0:
//                                noScrollViewPager.setCurrentItem(position, false);
//                                break;
//                            case 1:
//                                noScrollViewPager.setCurrentItem(position, false);
//                                break;
//                            case 2:
//                                noScrollViewPager.setCurrentItem(2, false);
//                                break;
//                            case 3:
//                                noScrollViewPager.setCurrentItem(3, false);
//                                break;
//                        }
                    }

                    @Override
                    public void onTabReselect(int position) {
                    }
                });
//            }
//        },1000);

        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setCurrentTab(0);

    }

    @Override
    protected void setListener() {
        super.setListener();

    }

    public static class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public final static int TAB_COUNT = 4;

        @Override
        public Fragment getItem(int id) {
            switch (id) {
                case 0:
                    HomeFragment homeItemFragment = new HomeFragment();
                    return homeItemFragment;
                case 1:
                    MyShopFragment myMailFragment = new MyShopFragment();
                    return myMailFragment;
                case 2:
                    ShopCarFragment shopCarFragment = new ShopCarFragment();
                    return shopCarFragment;
                case 3:
                    MyCenterFragment myCenterFragment = new MyCenterFragment();
                    return myCenterFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            return TAB_COUNT;
        }
    }
}
